package com.doctorliang.cheng.shopping.type.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.doctorliang.cheng.shopping.R;
import com.doctorliang.cheng.shopping.app.GoodsInfoActivity;
import com.doctorliang.cheng.shopping.home.adapter.HomeFragmentAdapter;
import com.doctorliang.cheng.shopping.home.bean.GoodsBean;
import com.doctorliang.cheng.shopping.type.bean.TypeBean;
import com.doctorliang.cheng.shopping.utils.Constants;
import com.doctorliang.cheng.shopping.utils.ToastUtil;

import org.xutils.common.util.DensityUtil;

import java.util.List;

/**
 * Created by YBH on 2017/12/14.
 */

public class TypeRightAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private  Context mcontext;
    private LayoutInflater layoutInflater;
    //常用分类
    private List<TypeBean.ResultBean.ChildBean> child = null;

    //热卖推荐
    private List<TypeBean.ResultBean.HotProductListBean> hot_product_list = null;

    /**
     * 热卖推荐类型
     */
    public static final int HOT = 0;
    /**
     * 常用分类类型
     */
    public static final int ORDINARY = 1;


    /**
     * 当前的类型
     */
    public int currentType;


    public TypeRightAdapter(Context mcontext, List<TypeBean.ResultBean> result) {
        this.mcontext = mcontext;
        if (result!=null&&result.size()>0) {
            child = result.get(0).getChild();
            hot_product_list = result.get(0).getHot_product_list();
        }
        layoutInflater = LayoutInflater.from(mcontext);
    }


    @Override
    public int getItemViewType(int position) {
        if (position == HOT) {
            currentType = HOT;
        } else {
            currentType = ORDINARY;
        }
        return currentType;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == HOT) {
            return new HotViewHolder(layoutInflater.inflate(R.layout.item_hot_right, null), mcontext);
        } else {
            return new OrdinaryViewHolder(layoutInflater.inflate(R.layout.item_ordinary_right, null), mcontext);

        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == HOT) {
            HotViewHolder hotViewHolder = (HotViewHolder) holder;
            //设置数据
            hotViewHolder.setData(hot_product_list);
            Log.e("tag",child.size()+"");
            Log.e("tag","position="+position);
        } else {
            OrdinaryViewHolder ordinaryViewHolder = (OrdinaryViewHolder) holder;
            //设置数据
//            ordinaryViewHolder.setData(child.get(position),position);
            ordinaryViewHolder.setData(child.get(position - 1), position-1);//上面有热卖推荐？
        }
    }

    @Override
    public int getItemCount() {
//        return child.size()-1;
        return child.size() + 1;
    }

    /**
     * 热卖
     */
    private class HotViewHolder extends RecyclerView.ViewHolder {
        private  Context mcontext;
        private final LinearLayout linear;

        public HotViewHolder(View inflate, Context mcontext) {
            super(inflate);
            this.mcontext = mcontext;
            linear = inflate.findViewById(R.id.linear);
        }

        public void setData(final List<TypeBean.ResultBean.HotProductListBean> hot_product_list) {
            for (int i=0;i<hot_product_list.size();i++) {
                //实例化布局参数
                LinearLayout.LayoutParams lineLp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                //new一个线性布局
                final LinearLayout myLinear = new LinearLayout(mcontext);
                //设置方向
                myLinear.setOrientation(LinearLayout.VERTICAL);
                //设置外边距（做适配）
                lineLp.setMargins(DensityUtil.dip2px(5),0,DensityUtil.dip2px(5), DensityUtil.dip2px(20));

                //添加为布局孩子
                linear.addView(myLinear,lineLp);

                //设置图片的大小
                LinearLayout.LayoutParams ivlp = new LinearLayout.LayoutParams(DensityUtil.dip2px(80), DensityUtil.dip2px(80));
                //设置距离底部有10个dp
                ivlp.setMargins(0,0,0,DensityUtil.dip2px(10));
                ImageView imageview = new ImageView(mcontext);

                //请求图片
                Glide.with(mcontext).load(Constants.BASE_URl_IMAGE+hot_product_list.get(i).getFigure()).into(imageview);
                //添加到myLinear里面
                myLinear.addView(imageview,ivlp);


                //设置价格
                LinearLayout.LayoutParams textViewLp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                TextView textView = new TextView(mcontext);
                textView.setText("￥" + hot_product_list.get(i).getCover_price());
                textView.setGravity(Gravity.CENTER);
                textView.setTextColor(Color.parseColor("#ed3f3f"));

                //添加到myLinear里面
                myLinear.addView(textView,textViewLp);

                //给子布局设置一个tag(因为每一个线性布局就代表一个子布局（图片+价格，后面需要点击进入详情 用来记录当前的孩子位置）
                myLinear.setTag(i);

                //设置点击事件
                myLinear.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //拿出位置
                        int i = (int) myLinear.getTag();
                        //取得详情内容里面的东西
                        String cover_price = hot_product_list.get(i).getCover_price();
                        String name = hot_product_list.get(i).getName();
                        String figure = hot_product_list.get(i).getFigure();
                        String product_id = hot_product_list.get(i).getProduct_id();
                        //把以上商品信息的封装起来(封装类必须序列化 后面传参数要用)
                        GoodsBean goodsBean = new GoodsBean(name, cover_price, figure, product_id);

                        //打开商品详情页面 要携带参数
                        Intent intent = new Intent(mcontext, GoodsInfoActivity.class);
                        intent.putExtra(HomeFragmentAdapter.GOODS_BEAN,goodsBean);
                        mcontext.startActivity(intent);
                        Toast.makeText(mcontext, "position" + i, Toast.LENGTH_SHORT).show();
                    }
                });


            }
        }
    }

    /**
     * 普通分类
     */
    private class OrdinaryViewHolder extends RecyclerView.ViewHolder {
        private  Context mcontext;
        private ImageView iv_ordinary_right;
        private TextView tv_ordinary_right;
        private LinearLayout ll_root;

        public OrdinaryViewHolder(View inflate, Context mcontext) {
            super(inflate);
            this.mcontext = mcontext;
            iv_ordinary_right = (ImageView) inflate.findViewById(R.id.iv_ordinary_right);
            tv_ordinary_right = (TextView) inflate.findViewById(R.id.tv_ordinary_right);
            ll_root = (LinearLayout) inflate.findViewById(R.id.ll_root);
        }

        public void setData(TypeBean.ResultBean.ChildBean childBean, final int i) {
            //加载图片
            Glide.with(mcontext).load(Constants.BASE_URl_IMAGE+childBean.getPic()).into(iv_ordinary_right);

            //设置名称
            tv_ordinary_right.setText(childBean.getName());

//            //取得详情内容里面的东西
//            String cover_price = childBean.
//            String name = hot_product_list.get(i).getName();
//            String figure = hot_product_list.get(i).getFigure();
//            String product_id = hot_product_list.get(i).getProduct_id();
//            //把以上商品信息的封装起来(封装类必须序列化 后面传参数要用)
//            GoodsBean goodsBean = new GoodsBean(name, cover_price, figure, product_id);


            //点击事件
            ll_root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ToastUtil.show(mcontext,"position="+i,Toast.LENGTH_SHORT);
                }
            });
        }
    }
}
